
/**
 * Queue.
 * @param <T> Generic type.
 * @author Lukas Koell
 * @author David Medenjak
 * @author Johannes Riedmann
 */
public class Queue<T> implements Container<T> {

    /**
     * References to the first and last item of Queue
     */
    private LinkedItem<T> first;
    private LinkedItem<T> last;

    /**
     * Adds an item to the container at last.
     * @param item Item to add.
     */
    public void push(T item) {
        if (first == null) {
            first = new LinkedItem<T>(item);
            last = first;
        } else {
            LinkedItem<T> newItem = new LinkedItem<T>(item);
            last.setNext(newItem);
            last = newItem;
        }
    }

    /**
     * Gets the first element from the container and removes it.
     * @return Next element.
     */
    public T pop() {
        LinkedItem<T> element = first;
        if (first == last) {
            last = null;    // if first is the only item in queue set last null
        }
        first = first.getNext();
        return element.getValue();
    }

    /**
     * Checks if an item is left.
     * @return True if it's empty, false if it's not.
     */
    public boolean isEmpty() {
        if (first == null) {
            return true;
        }
        return false;
    }

    /**
     * Gets the next element from the container.
     * @return Next element.
     */
    public T peek() {
        return first.getValue();
    }
}
